<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/6/006
 * Time: 11:30
 */

namespace app\api\service;

use app\api\model\Attribute as AttributeModel;
use app\api\model\AttrValue as AttrValueModel;
use app\api\model\ProductAttr as ProductAttrModel;

class Product {

    /**
     * 获取定制商品属性
     * @param $data
     * @return array|\PDOStatement|string|\think\Collection
     */
    public static function productCustomizationAttr($data) {
        $attr = AttributeModel::getAttrByIds($data->attr);
        $attr_value = AttrValueModel::getAttrValueByIds($data->attr_value);

        foreach ($attr as $key => &$val) {
            $attr_v = [];
            foreach ($attr_value as $k => $v) {
                if ($v['attribute_id'] == $val['id']) {
                    // 有些老图片太大 处理一下
                    if (is_file('./' . $v['image'])) {
                        $image = \think\Image::open('./' . $v['image']);
                        if ($image->width() > 200) {
                            $image->thumb(200, 200)->save('./' . $v['image']);
                        }
                    }
                    $attr_v[] = $v;
                }
            }
            if (!$attr_v) {
                unset($attr[$key]);
                continue;
            }

            $val['value'] = $attr_v;
        }

        return $attr;
    }

    /**
     * 获取销售产品属性
     * @param $data
     * @return array
     */
    public static function productMarketAttr($data) {
        $product_attr_group = ProductAttrModel::getProductAttrByProductId($data->id);

        $attrs = '';
        foreach ($product_attr_group as $k => $v) {
            $attrs .= $v->attr_group . ',';
        }
        $attrs = substr($attrs, 0, -1);

        //  价格区间
        $price_scope = $data->price;
        if ($attrs) {
            $max = $product_attr_group[0]->price;
            $min = end($product_attr_group);
            $min = end($min);
            if ($min->price == $max) {
                $price_scope = $max;
            } else {
                $price_scope = $min->price . '~' . $max;
            }
        }

        $attr = AttributeModel::all(['type' => 2]);

        $product_attr = [];
        if ($attr) {
            foreach ($attr as $k => $v) {
                $attr_value = $v->attrValue()->where('id', 'in', $attrs)->select();
                $value = [];
                foreach ($attr_value as $av) {
                    if ($av['attribute_id'] == $v['id']) {
                        $value[] = [
                            'name' => $av['name'],
                            'id' => $av['id'],
                            'image' => $av['image'],
                            'select' => true,
                            'selected' => false
                        ];
                    }
                }

                if ($value) {
                    $product_attr[] = [
                        'id' => $v['id'],
                        'name' => $v['name'],
                        'value' => $value
                    ];
                } else {
                    unset($attr[$k]);
                }
            }
        }
        $product_attr_group_json = [];
        foreach ($product_attr_group as $v) {
            $attr_group = explode(',', $v['attr_group']);
            $product_attr_group_json_data = [];
            foreach ($attr_group as $av) {
                $product_attr_group_json_data[] = $av;
            }
            $product_attr_group_json_data[] = $v['price'];
            $product_attr_group_json_data[] = $v['inventory'];
            $product_attr_group_json_data[] = $v['image'];
            $product_attr_group_json_data[] = $v['id'];

            $product_attr_group_json[] = $product_attr_group_json_data;
        }

        return [
            'attr' => $product_attr,
            'price_scope' => $price_scope,
            'product_attr_group' => $product_attr_group_json
        ];
    }

}